package com.halflife.comeElectricity.member.rider;

import com.halflife.framework.dto.Result;
import com.halflife.comeElectricity.orderMng.history.HistoryOrderRepository;
import com.halflife.comeElectricity.orderMng.history.PayInfo;
import com.halflife.comeElectricity.orderMng.order.OrderEnum;
import com.halflife.shiro.utils.ShiroUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhaoyf on 2018/2/4.
 */
@RestController
@RequestMapping("rider_rider")
public class RiderController {
    @Resource
    private HistoryOrderRepository historyOrderRepository;
    @Resource
    private RiderRepository riderRepository;
    @Resource
    private PayLogRepository payLogRepository;

    @GetMapping("/{id}")
    public Rider getRiderById(@PathVariable Integer id){
        return riderRepository.selectByPrimaryKey(id);
    }
    @GetMapping("/mymoney")
    public Result mymoney(@RequestParam(value="currentPage",defaultValue = "0") Integer currentPage,
                          @RequestParam(value="pageSize",defaultValue = "30") Integer pageSize, HttpServletRequest request) {
        Rider rider = ShiroUtil.getRider(request);
        if (rider == null) {
            return Result.Error("您没有登陆,请退出系统重新登陆");
        }

        BigDecimal collectdMoney = payLogRepository.getMyMoney(rider.getId());
        BigDecimal totalMoney = historyOrderRepository.getMyMoney(rider.getId(), OrderEnum.PAYED.m_orderStatus.byteValue());
        if (totalMoney == null) {
            totalMoney = new BigDecimal("0");
        }
        if (collectdMoney == null) {
            collectdMoney = new BigDecimal("0");
        }
        Map param = new HashMap();
        param.put("riderId", rider.getId());
        int totalCount = payLogRepository.selectCount(param);
        param.put("currentPage", (currentPage - 1) * pageSize);
        param.put("pageSize", pageSize);
        List<PayInfo> infoList = payLogRepository.selectListByPage(param);
        int totalPage = totalCount / pageSize;
        if (totalCount % pageSize != 0)
            totalPage++;
        Map<String, Object> map = new HashMap<>();
        map.put("totalMoney", totalMoney);
        map.put("collectdMoney", collectdMoney);
        map.put("leaveMoney", totalMoney.subtract(collectdMoney));
        map.put("payLogs", infoList);
        Result result = Result.Ok(map);
        result.setTotalCount(totalCount);
        result.setTotalPage(totalPage);
        return result;
    }
    @GetMapping("")
    public Rider getRider(HttpServletRequest request){

        //获取当前用户
        Rider rider  =  ShiroUtil.getRider(request);
        return riderRepository.selectByPrimaryKey(rider.getId());
    }

    @GetMapping("/singleByWechat/{uionid}")
    public Rider getRiderByWechat(@PathVariable String uionid, @RequestParam String name){
        // TODO: 2019/2/9
        Map riderMap = new HashMap();
        riderMap.put("unionid", uionid);
        List<Rider> riderList = riderRepository.selectAll(riderMap);
        return riderList.get(0);
    }

    @PostMapping("")
    public ResponseEntity addRider(@RequestBody Rider rider, HttpServletRequest request){
        Integer id = riderRepository.insert(rider);
        return new ResponseEntity(rider, HttpStatus.OK);
    }

    @PutMapping("")
    public ResponseEntity updateRider(@RequestBody Rider rider, HttpServletRequest request){
        Integer id = riderRepository.updateByPrimaryKey(rider);
        return new ResponseEntity(rider, HttpStatus.OK);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity updateRider(@PathVariable Integer id, HttpServletRequest request){
        Integer key = riderRepository.deleteByPrimaryKey(id);
        return new ResponseEntity(key, HttpStatus.OK);
    }
}
